package com.example.demo.service.impl;

import com.example.demo.commen.ResultCode;
import com.example.demo.dao.MessageMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Message;
import com.example.demo.model.User;
import com.example.demo.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
   private UserServiceImpl userService;
    @Override
    public void insertMessage(Message message) {
        System.out.println(message);
        if(message==null|| message.getReceiveUserId()==null||message.getReceiveUserId()<=0||
        message.getContent()==null||!StringUtils.hasLength(message.getContent())||message.getPostUserId()==null||
        message.getPostUserId()<=0){
            throw  new ApplicationException("参数校验失败");

        }

        User postUser =  userService.getUserById(message.getPostUserId());
        User receiveUser = userService.getUserById(message.getReceiveUserId());
        if(postUser==null||postUser.getId()<=0||postUser.getDeleteState()==1||
            receiveUser==null||receiveUser.getId()<=0||receiveUser.getDeleteState()==1){
            throw new ApplicationException("发送者不存在或接收者不存在");
        }
        Message updateMessage = new Message();
        updateMessage.setPostUserId(postUser.getId());
        updateMessage.setReceiveUserId(receiveUser.getId());
        updateMessage.setContent(message.getContent());
        updateMessage.setState((byte)0);
        updateMessage.setDeleteState((byte)0);
        updateMessage.setCreateTime(new Date());
        updateMessage.setUpdateTime(new Date());
      int flag =  messageMapper.insertSelective(updateMessage);
      if(flag!=1){
          throw new ApplicationException(ResultCode.FAILED.getMessage());
        }

    }

    @Override
    public Integer getUnReadMessage(Long receiveUserId) {
          if(receiveUserId==null||receiveUserId<=0){
              throw  new ApplicationException("参数校验失败");
          }

        User user = userService.getUserById(receiveUserId);
          if(user==null||user.getId()<=0||user.getDeleteState()==1){
              throw new ApplicationException("用户不存在");
          }
        System.out.println(user);
          Integer unReadMessageCount = messageMapper.selectUnreadMessage(user.getId());
        System.out.println(unReadMessageCount+"dadadadasda");
          return unReadMessageCount;
    }

    @Override
    public List<Message> getMessageByReceiveId(Long receiveUserId) {
        if(receiveUserId==null||receiveUserId<=0){
            throw new ApplicationException("参数校验失败");
        }
        User user = userService.getUserById(receiveUserId);
        if(user==null||user.getId()<=0){
            throw new ApplicationException("用户不存在");
        }

      List<Message>  lists = messageMapper.selectMessageByReceiveUserId(receiveUserId);
        return lists;
    }

    @Override
    public void updateMessageState(Long messageId,Byte state) {
        if(messageId==null||messageId<=0||state==null||state<0||state>2){
            throw  new ApplicationException("帖子不存在或者状态出错");
        }
        Message message = messageMapper.selectByPrimaryKey(messageId);
        if(message==null||message.getDeleteState()==1){
            throw new ApplicationException("帖子不存在或帖子被删除了");
        }
        Message updateMessage = new Message();
        updateMessage.setId(message.getId());
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
      int flag =   messageMapper.updateByPrimaryKeySelective(updateMessage);
      if(flag!=1){
          throw new ApplicationException("站内信状态更新失败");
      }
    }

    @Override
    public Message selectMessageById(Long messageId) {
        if(messageId==null||messageId<=0){
            throw new ApplicationException("参数校验失败");
        }
        Message message = messageMapper.selectByPrimaryKey(messageId);
        if(message==null||message.getDeleteState()==1||message.getId()<=0){
            throw new ApplicationException("帖子不存在");
        }
        return message;
    }
}
